﻿

using K4os.Compression.LZ4.Internal;

public class NetHandler_AddMoment : GameServerMessageHandler
{
    public const string Key = "AddMoment";

    public static NetHandler_AddMoment NewHandler()
    {
        return new NetHandler_AddMoment();
    }

    protected override ISerialize NewData() => new AddMomentRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        AddMomentRequestData requestData = _data as AddMomentRequestData;

        GameDebug.Log($"用户{client.userID}  添加动态:{requestData?.userId}");

        UserServer user = Game.instance.GetUser(client.userID);

        AddMomentResponseData response = new AddMomentResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                long deviceId = user.data.id;
                long time = (long)CommonFunctions.GetTimeStampSeconds();
                users ut = UserServer.FindInUserTable(deviceId);

                long momentId = moments.AddMoment(deviceId, ut.location_city, time, requestData.content
                    ,requestData.location, requestData.topic, requestData.momentStatus, "", "");

                string[] texUrls = new string[requestData.texs.Length];

                for (int i = 0; i < requestData.texs.Length; i++)
                {
                    if (Tex.IsNull(requestData.texs[i]))
                        continue;

                    TextureSaver.SaveMomentPhoto(deviceId, momentId, i + 1, requestData.texs[i]);

                    texUrls[i] = TextureSaver.GetMomentPhotoUrl(deviceId, momentId, i + 1);
                }

                Moment moment = new Moment(deviceId, momentId, time, requestData.content, requestData.location,
                    requestData.topic, null, null, texUrls);

                response.moment = moment;

                response.code = 0;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
